package com.igl.talk.android.client.ide.notifications;/*
 * @author Adam Wieczorek 
 *
 * 22/04/2014
 *
 * All rights reserved.
 */

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import com.igl.talk.android.client.R;
import com.igl.talk.android.client.ide.activities.MainViewActivity;

public enum NotificationsFactory {
    NEW_MESSAGE {

        private final int NOTIFICATION_ID = 337;

        @Override
        public int notify(Context context, int messagesCounter) {
            final NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            // show notification
            notificationManager.notify(NOTIFICATION_ID, getBuilder(context, messagesCounter).build());
            return NOTIFICATION_ID;
        }

        @Override
        public void updateNotification(Context context, final int id, int messagesCounter) {
            Notification.Builder builder = getBuilder(context, messagesCounter);
            // update notification
            final NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(id, builder.build());
        }

        private Notification.Builder getBuilder(Context context, int messagesCounter) {
            // Creating resources for notification builder
            String content = context.getResources().getQuantityString(R.plurals.notif_unread_messages_content,
                    messagesCounter, messagesCounter);
            String title = context.getResources().getQuantityString(R.plurals.notif_unread_messages_title,
                    messagesCounter, messagesCounter);
            // Preparing notification
            Notification.Builder builder = new Notification.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_notify_msg)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL);
            // Creating intent to redirect to when user taps on notification
            Intent intent = new Intent(context, MainViewActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
            builder.setContentIntent(contentIntent);

            return builder;
        }
    };

    public abstract int notify(Context context, int messagesCounter);

    public abstract void updateNotification(Context context, int id, int messagesCounter);
}
